<?php
/**
 * 权限
 */
namespace app\model;

use think\Model;
use app\model\Role;


class Admin extends Model
{
  /**
    * 管理员列表
    *
    */
   public function GetList($where){
    $admin=new Admin;
    $result=$admin->field('id,nickname,mobile,status,create_time')->where($where)->paginate()->toArray();
    $Role=new Role();
    foreach ($result['data'] as &$val){
    	$val['role_name']=$Role->GetRoleNameByUserId($val['id']);
    }
    return $result;
   }
   /**
    * 管理员详情
    *
    */
   public function detail($id){
      $admin = Admin::find($id);
      return $admin;
   }
  /**
   * 添加
   *
  */
   public function AddData($param){
      $admin = new Admin;
      $param['create_time']=date('Y-m-d H:i:s');

      $field=['nickname','mobile'];
      if(isset($param['password'])&&!empty($param['password'])){
         $field[]='password';
         $param['password']=md5($param['password']);
      }
      
      $admin->allowField($field)->save($param);
      return $admin->id; 
   }
   /**
   * 编辑
   *
  */
  public function EditData($param,$id){
   $admin = Admin::find($id);
   $param['update_time']=date('Y-m-d H:i:s');

   $field=['nickname','mobile'];
   if(isset($param['password'])&&!empty($param['password'])){
      $field[]='password';
      $param['password']=md5(trim($param['password']));
   }
   $admin->allowField( $field)->save($param);
   return $id; 
  }
  /**
   * 修改禁用状态
   *
  */
  public function UpdateStatus($id){
   $admin = Admin::find($id);
   $admin->status=$admin->status==1?0:1;
   $admin->save();
   return true; 
  }
 /**
   * 删除
   *
  */
  public function Del($param){
   $update=[];
   foreach($param as $val){
       $update[]=['id'=>$val,'status'=>-1,'delete_time'=>date('Y-m-d H:i:s')];
   }
   if(!empty($update)){
      $admin = new Admin;
      $admin->saveAll($update);
   }
   return true; 
  }
}